package hikari.instruments;

import com.gooddies.wiring.annotations.PostWiring;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.managers.Manager3D;
import org.swig.jirr.ISceneNode;
import org.swig.jirr.line3df;
import org.swig.jirr.vector2di;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
@WiringComponent(singleton = true, lazy = true)
public class NormalInstrument extends SelectInstrument {

    /*    private vector3df p1;
     private vector3df p2;*/
    private vector3df position;
    private ISceneNode sphere;
    @Wire
    private Manager3D manager3D;

    private NormalInstrument() {
        /*ICustomizableTriangleMeshNode node = Manager3D.getInstance().getSceneManager().addICustomizableTriangleMeshNode(Manager3D.getInstance().getSceneManager().getRootSceneNode());
         node.setVertices(new S3DVertex[]{
         new S3DVertex(0, 0, 5, 1, 1, 0, SColor.RED, 0, 1),
         new S3DVertex(10, 0, -5, 1, 0, 0, SColor.RED, 1, 1),
         new S3DVertex(0, 5, 0, 0, 1, 1, SColor.GREEN, 1, 0),
         new S3DVertex(-5, 0, -5, 0, 0, 1, SColor.BLUE, 0, 0),});
         node.setIndices(new int[]{0, 2, 3, 2, 1, 3, 1, 0, 3, 2, 0, 1});
         node.recalculate();
         Cube3dObject obj=new Cube3dObject(Manager3D.getInstance().getSceneManager());
         obj.getMeshSceneNode().remove();
         obj.setMeshSceneNode(node);
         Manager3D.getInstance().AddObject(obj);*/
    }

    @Override
    public void instrumentActivate() {
        sphere = manager3D.getSceneManager().addSphereSceneNode(0.5f);
        sphere.setVisible(false);
        super.instrumentActivate();
    }

    @Override
    public void instrumentDeactivate() {
        if(sphere!=null){
            sphere.remove();
        }
    }
    
    
    
    
    

    @Override
    public void mouseMove(int x, int y, int dx, int dy, int modifier) {
        line3df line = manager3D.getSceneManager().getSceneCollisionManager().getRayFromScreenCoordinates(new vector2di(x, y));
        float px;
        float py = 0;
        float pz;
        float x1 = line.getStart().getX();
        float x2 = line.getEnd().getX();
        float y1 = line.getStart().getY();
        float y2 = line.getEnd().getY();
        float z1 = line.getStart().getZ();
        float z2 = line.getEnd().getZ();
        if (y2 - y1 == 0) {
            position = null;
            return;
        }
        float coeficient = (0 - y1) / (y2 - y1);
        px = coeficient * (x2 - x1) + x1;
        pz = coeficient * (z2 - z1) + z1;
        position = new vector3df(px, py, pz);
        /*super.MouseMove(x, y, dx, dy, modifier);
         ISceneCollisionManager cm = Manager3D.getInstance().getSceneManager().getSceneCollisionManager();
         line3df line = cm.getRayFromScreenCoordinates(new vector2di(x, y));
         vector3df point = new vector3df();
         triangle3df triangle = new triangle3df();
         ISceneNode node = cm.getSceneNodeAndCollisionPointFromRay(line, point, triangle);
         if (node == null) {
         p1 = null;
         p2 = null;
         } else {
         p1 = new vector3df(point.getX(), point.getY(), point.getZ());
         vector3df normal = triangle.getNormal().normalize().timesOperator(3.0f);
         p2 = new vector3df(p1.getX(), p1.getY(), p1.getZ());
         p2 = p2.addOperator(normal);
         }*/
        super.mouseMove(x, y, dx, dy, modifier);
    }

    @Override
    public void onBeforeRender() {
        if (position == null) {
            sphere.setVisible(false);
        } else {
            sphere.setPosition(position);
            sphere.setVisible(true);
        }

        super.onBeforeRender();
    }

    /*@Override
     public void onAfterRenderBeforeGui() {
     super.onAfterRenderBeforeGui();
     if (p1 != null) {
     Manager3D.getInstance().getVideoDriver().draw3DLine(p1, p2);
     }
     }*/
}